<?php
/**
 * Class that will translated the aplication
 *
 * @author Renan Henrique Abreu <renanhabreu@gmail.com>
 * @package Nucleo
 * @subpackage Nucleo.Language
 * @copyright 2009-2010 Renan Abreu
 */
abstract class Nucleo_Language_Translate {
    /**
     * Phrases for application
     * @var Array
     */
    static private $translation 	 = null; //Array
    static private $locale 		 = 'en';
    static private $file_translation     = false;
    static private $translation_sifra    = null; //Array

    /**
     * Set a locale for application
     * @param string
     * @param boolean Read a XML
     */
    static public function setLocale($locale,$readFile = true) {
        self::$locale = $locale;
        if($readFile && !self::$file_translation) {
            if(file_exists(LANGUAGE_PATH.$locale.'.php')) {
                include(LANGUAGE_PATH.$locale.'.php');
                self::$translation = $TRANSLATE;
                self::$file_translation = true;
            }
        }
    }

    /**
     * Add phrases
     * @param Array Array('id'=>'phrase')
     */
    static public function addPhrases($phrases) {
        if(is_array($phrases))
            self::$translation = $phrases;
        else
            throw new Nucleo_Exception_Sifra('006',true);
    }

    /**
     * Get a translation for the phrase
     * @param string
     * @return string
     */
    static public function getPhrase($id) {
        if(isset(self::$translation)) {
            $return = self::$translation[$id];
        }else {
            throw new Nucleo_Exception_Sifra('025',true);
        }

        return $return;
    }

    /**
     * Get a phrase translated
     * @param string
     * @return string
     */
    static public function sifraMsg($id) {
        if(!isset(self::$translation_sifra)){
            $file = LOCAL_FRAMEWORK.'Nucleo/Language/Lang/'.self::$locale.'.php';
            $default = LOCAL_FRAMEWORK.'Nucleo/Language/Lang/en.php';
            
            if(file_exists($file)) {
                include($file);
                self::$translation_sifra = $TRANSLATE;

            }elseif(file_exists($default)){
                include($default);
                self::$translation_sifra = $TRANSLATE;

            }else{
                throw new Nucleo_Exception_Load('026');
            }
        }

        return self::$translation_sifra[$id];

    }
}
?>